package com.leo.voice;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class VoiceDesktop extends Activity {
    /** Called when the activity is first created. */
	GridView mGridView;
	VoiceApplication mVoiceApplication;
	private static String TAG="VoiceDesktop";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.desktop);
        loadFromXml();
    }
    
    public void loadFromXml(){
    	mGridView = (GridView) findViewById(R.id.gridview);
    	mVoiceApplication = (VoiceApplication) this.getApplication();
    	//if( mGridView == null )
    	//	return;
    	mGridView.setAdapter(new DesktopGridAdapter(this, mVoiceApplication.getLaunchedAppInfo()));
    	Log.i(TAG, "loadFromXML");
    	mGridView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				//Toast.makeText(VoiceDesktop.this, "test", Toast.LENGTH_LONG).show();
				//((TextView)mGridView.getChildAt(arg2)).setText("Select"+arg2);
				Log.i(TAG, "arg2="+arg2+"arg3"+arg3);
				DesktopGridItem item = (DesktopGridItem) arg0.getItemAtPosition(arg2);
				Intent itent = item.getAppLauncher();
				startActivity(itent);
			}
           
        });

    }
    
    //Handle the Key event, e.g Back, home's up or down
    public boolean  dispatchKeyEvent  (KeyEvent  event){
    	if(event.getAction() == KeyEvent.ACTION_DOWN){
    		if(event.getKeyCode() == KeyEvent.KEYCODE_BACK)
    			return true;
    	}else if(event.getAction() == KeyEvent.ACTION_UP){
    		if(event.getKeyCode() == KeyEvent.KEYCODE_BACK ){
    			return true;
    		}
    	}
    	
		return super.dispatchKeyEvent(event);	
    }
    
    
    public void getAppList(){
    	
    }
    
    
    class TextAdapter extends BaseAdapter{
    	private Context mContext;
    	private ArrayList<TextView> mList;
    	private VoiceApplication mApplication;
    	public TextAdapter(Context c){
    		Log.i(TAG, "TextAdapter()");
    		mContext = c;
    		mApplication = (VoiceApplication)getApplication();
    		mList = dummyTextView();
    	}
		public int getCount() {
			// TODO Auto-generated method stub
		
			return mList.size();
		}

		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub	
			return mList.get(position);
		}
		
		private ArrayList<TextView> dummyTextView(){
			ArrayList<TextView> l = new ArrayList();
			List<ApplicationInfo> allInfo = mApplication.getAllInstalledAppInfo();
			
			for( ApplicationInfo info : allInfo){
				TextView tv = new TextView(mContext);
				tv.setText(info.loadLabel(getPackageManager()));
				tv.setBackgroundColor(Color.GRAY);
				tv.setTextColor(Color.GREEN);
				l.add(tv);
			}
			return l;	
		}
    	
    }
    
}